<?php
use yii\helpers\Html;
use yii\web\View;
use backend\models\activeRecords\SpreadInfo;
use kartik\widgets\ActiveForm;
use yii\helpers\Url;
use kartik\builder\FormGrid;
use kartik\builder\Form;
use common\libs\Constants;
use backend\models\activeRecords\GuildGame;

/* @var $this View */
/* @var $model SpreadInfo */
/* @var $form ActiveForm */
?>
<?php $form = ActiveForm::begin([
    'id' => 'spread-info-form',
    'type' => ActiveForm::TYPE_VERTICAL,
    'options' => [
        'enctype' => 'multipart/form-data'
    ],
    'enableAjaxValidation' => true,
    'validationUrl' => Url::toRoute(['validate-form'])
]);
$js = <<<JS
    var game_id = $('#spreadinfo-game_id option:selected').val();
    $.ajax({
        url:'/spread/spread-info/get-server-list',
        type:'post',
        dataType:'text',
        data:{game_id:game_id},
        async:false,
        success:function(data){
            $("#spreadinfo-server_id").append(data);
        }
    });

    $("#spreadinfo-game_id").click(function(){
        game_id = $('#spreadinfo-game_id option:selected').val();
        $.ajax({
            url:'/spread/spread-info/get-server-list',
            type:'post',
            dataType:'text',
            data:{game_id:game_id},
            async:false,
            success:function(data){
                $("#spreadinfo-server_id").empty();
                $("#spreadinfo-server_id").append(data);
            }
        });
    });


JS;
$this->registerJs($js);

echo FormGrid::widget([
    'model' => $model,
    'form' => $form,
    'autoGenerateColumns' => true,
    'rows' => [
        [
            'attributes' => [
                'game_id' => [
                    'type' => Form::INPUT_DROPDOWN_LIST,
                    'items'=> GuildGame::getGameList()
                ],
            ]
        ],
        [
            'attributes' => [
                'server_id' => [
                    'type' => Form::INPUT_DROPDOWN_LIST,
                    'items'=>[]
                ],
            ]
        ],
        [
            'attributes' => [
                'actions' => [    // embed raw HTML content
                    'type' => Form::INPUT_RAW,
                    'value' => '<div style="text-align: center;">' .
                        Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) .
                        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .
                        Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-primary']) .
                        '</div>'
                ],
            ]
        ]
    ]
]);
ActiveForm::end();